// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import axios from 'axios'
import router from './router'

import store from './Store'

import messageBox from '@/components/message/index'
import Popup from '@/components/popup/index'
// 自己写的信息框
Vue.prototype.$message = messageBox
// 自己写的弹窗
Vue.prototype.$popup = Popup.install

Vue.prototype.axios = axios

/* 祖孙传值 父传子 */
Vue.prototype.$dispatch = function (eventName, data) {
  let parent = this
  // 查找父元素
  while (parent) {
    if (parent) {
      // 父元素用$emit触发
      parent.$emit(eventName, data)
      // 递归查找父元素
      parent = parent.$parent
    } else {
      break
    }
  }
}

/* 祖孙传值 子传父 */
Vue.prototype.$boardcast = function (eventName, data) {
  boardcast.call(this, eventName, data)
}
function boardcast (eventName, data) {
  this.$children.forEach(child => {
    // 子元素触发$emit

    child.$emit(eventName, data)
    if (child.$children.length) {
      // 递归调用，通过call修改this指向 child
      boardcast.call(child, eventName, data)
    }
  })
}
/* 没啥关系传值 新建实例 */
Vue.prototype.$bus = new Vue()

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
